﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using wAppStoreWebAPI;
using wAppStoreWebAPI.wClasses;
using wCoreX;

namespace wStore.Model
{
  public class wAppModel : BindableBase
  {
    static wAppModel()
    {
      I = new wAppModel();
    }
    public wAppModel()
    {
      selCategory = CategoryList[0];
      if (PlatformList.Count > 0)
      {
        SelectedPlatform = PlatformList.FirstOrDefault(c => c.Title == "Windows");
      }
    }
    public static wAppModel I { get; private set; }

    public virtual void LoadApp()
    {
      try
      {
        wCategory[] cat = null;
        if (selCategory != null)
        {
          cat = new wCategory[1];
          cat[0] = SelectedCategory;
        }

        var res = wAppStoreAPI.GetAppsList(SelectedPlatform, false, cat, false, SearchText);
        

        appList.Clear();
        appList.AddRange(res.Select(c => new wAppStoreItem(c)));
        OnPropertyChanged("AppList");
      }
      catch (Exception ex)
      {
        throw ex;
      }
    }

    ObservableCollection<wAppStoreItem> appList = new ObservableCollection<wAppStoreItem>();
    public ObservableCollection<wAppStoreItem> AppList { get { return appList; } }

    wPlatforms platformList;
    public wPlatforms PlatformList
    {
      get
      {
        if (platformList == null) platformList = wAppStoreAPI.getPlatfroms();
        return platformList;
      }
    }
    
    wPlatform selPlatform;
    public wPlatform SelectedPlatform
    {
      get { return selPlatform; }
      set
      {
        if (selPlatform != value)
        {
          selPlatform = value;
          OnPropertyChanged("SelectedPlatform");
          LoadApp();
        }
      }
    }

    string searchText = "";
    public string SearchText
    {
      get
      {
        return searchText;
      }
      set
      {
        if (searchText != value)
        {
          searchText = value;
          OnPropertyChanged("SearchText");
        }
      }
    }

    public wCategories CategoryList
    {
      get
      {
        var res = wAppStoreAPI.GetCategories();
        return res;
      }
    }
    wCategory selCategory = null;
    public wCategory SelectedCategory
    {
      get { return selCategory; }
      set
      {
        if (selCategory != value)
        {
          selCategory = value;
          OnPropertyChanged("SelectedCategory");
          LoadApp();
        }
      }
    }

    private ICommand loadAppCmd;
    public ICommand LoadAppCmd
    {
      get
      {
        return this.loadAppCmd ?? (this.loadAppCmd = new LoadAppCommand(this));
      }
    }

    public class LoadAppCommand : ICommand
    {
      public LoadAppCommand(wAppModel model)
      {
        this.model = model;
      }
      wAppModel model; 

      public bool CanExecute(object parameter)
      {
        return true;
      }

      public event EventHandler CanExecuteChanged;

      public void Execute(object parameter)
      {
        model.LoadApp();
      }
    }
  }
}
